package com.cjst.controller.api.image;

import java.io.File;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.cjst.controller.base.BaseController;
import com.cjst.service.intelligent.IntelligentControlService;
import com.cjst.util.FileToByte;
import com.cjst.util.common.ResultCode;
import com.cjst.util.json.ResultObj;

@Controller
@RequestMapping("/api/image")
public class ImageController extends BaseController{
	private static final Logger log = Logger.getLogger(ImageController.class);
	@Value("${img.location}")
	private String uploadDir;
	@Value("${file.path}")
	private String begin_url;
	
	
	@Autowired
	private IntelligentControlService intelligentControlService;
	@RequestMapping("/imageDiscern")
	@ResponseBody
	public String imageDiscern(MultipartFile file,Integer type){
		try{
			if(type==null){
				type=0;
			}
			//文件名称
            String fileName = file.getOriginalFilename();
            //文件后缀
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
            uploadDir = uploadDir + "/" + "baiduMind" + "/" + "image";
            //检查目录是否存在
            File dir = new File(uploadDir);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            //随机名称
            String randomFileName = UUID.randomUUID().toString();
            //文件存在服务器上的路径
            String fileUrl = uploadDir + "/" + randomFileName + "." + suffix;
            //将文件上传到文集的物理地址
            File serverFile = new File(fileUrl);
            file.transferTo(serverFile);
            Map<String, Object> map=intelligentControlService.imageControl(fileUrl, 0);
			ResultObj robj = new ResultObj(ResultCode.RESULT_SUCCESS, "识别完成",map);
			return robj.toString();
		}catch(Exception e){
			e.printStackTrace();
			ResultObj robj = new ResultObj(ResultCode.RESULT_NULL, "参数错误",null);
			return robj.toString();
		}
	}
	@RequestMapping(value = "/upImage", method = RequestMethod.POST)
	@ResponseBody
    public String upImage(MultipartFile file) {
		if(file==null) {
			ResultObj robj = new ResultObj(ResultCode.RESULT_EXCEPTION, "上传错误，file null",null);
        	//uploadDir="";
			return robj.toString(); 
		}
		
		try{
			//System.out.println("================uploadDir1sss:"+uploadDir);
			//文件名称
			String fileName = file.getOriginalFilename();
			//System.out.println("================uploadDir1:"+uploadDir);
			log.info("================uploadDir1:"+uploadDir);
        	//文件后缀
        	String suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
        	String uploadDir2 = uploadDir + "/" + "baidus";
        	log.info("================uploadDir2:"+uploadDir2);
        	//检查目录是否存在
        	File dir = new File(uploadDir2);
        	if (!dir.exists()) {
            	dir.mkdirs();
        	}
        	//随机名称
        	String randomFileName = UUID.randomUUID().toString();
        	//文件存在服务器上的路径
        	String  fileUrl= uploadDir2 + "/" + randomFileName + "." + suffix;
        	String fileUrl2 = uploadDir2 + "/ys_" + randomFileName + "." + suffix;
        	
        	String result_name="ys_" + randomFileName + "." + suffix;
        	
        	//将文件上传到文集的物理地址
        	File serverFile = new File(fileUrl);
        	file.transferTo(serverFile);
        	//图片压缩
        	FileToByte.commpressPicForScale(fileUrl, fileUrl2);
        	/*Thumbnails.of(fileUrl) 
            .scale(1f) 
            .outputQuality(0.5f) 
            .toFile(fileUrl2);*/
        	
        	log.info("2================uploadDir1:"+fileUrl);
        	log.info("2================uploadDir2:"+fileUrl2);
        	
        	//String result=begin_url + "/baidus/ys_" + randomFileName + "." + suffix;
        	log.info("================result:"+result_name);
        	ResultObj robj = new ResultObj(ResultCode.RESULT_SUCCESS, "识别完成",result_name);
        	//uploadDir="";
			return robj.toString(); 
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
    }
	@RequestMapping(value = "/imageSb", method = RequestMethod.POST)
	@ResponseBody
    public String imageSb(String fileDir,Integer type,Integer isBroad,String phone) {
		
		fileDir = uploadDir + "/"+"baidus"+"/" + fileDir;
		
		
		try{
			if(fileDir==null || fileDir.length()==0){
				ResultObj robj = new ResultObj(ResultCode.RESULT_NULL, "参数错误",null);
				return robj.toString();
			}
			if(isBroad==null){
				isBroad=0;
			}
			if(type==null){
				type=0;
			}
			Map<String, Object> map=intelligentControlService.imageControl(fileDir, 0);
			ResultObj robj = new ResultObj(ResultCode.RESULT_SUCCESS, "识别完成",map);
			log.info(robj.toString());
			return robj.toString();
		}catch(Exception e){
			e.printStackTrace();
			ResultObj robj = new ResultObj(ResultCode.RESULT_NULL, "参数错误",null);
			return robj.toString();
		}
    }
	@RequestMapping(value = "", method = RequestMethod.GET)
    public String toImage() {
        return "image";
    }
	@RequestMapping(value = "/deedImg", method = RequestMethod.POST)
	@ResponseBody
    public String deedImg(MultipartFile file) {
		try{
			//文件名称
            String fileName = file.getOriginalFilename();
            //文件后缀
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
            uploadDir = uploadDir + "/" + "tx" ;
            //检查目录是否存在
            File dir = new File(uploadDir);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            //随机名称
            String randomFileName = UUID.randomUUID().toString();
            //文件存在服务器上的路径
            String fileUrl = uploadDir + "/" + randomFileName + "." + suffix;
            String fileUrl2 = uploadDir + "/ys_" + randomFileName + "." + suffix;
            //将文件上传到文集的物理地址
            File serverFile = new File(fileUrl);
            file.transferTo(serverFile);
            //图片压缩
        	FileToByte.commpressPicForScale(fileUrl, fileUrl2);
			Map<String, Object> map=intelligentControlService.deedImg(FileToByte.readFile(fileUrl2));
			ResultObj robj = new ResultObj(ResultCode.RESULT_SUCCESS, "识别完成",map);
			uploadDir="";
			return robj.toString();
		}catch(Exception e){
			e.printStackTrace();
			ResultObj robj = new ResultObj(ResultCode.RESULT_NULL, "参数错误",null);
			return robj.toString();
		}
	}
}



